<?php
class Profile extends BaseEntity
{
    const DEFAULT_HEADSHOT_URL = 'http://ysq.lb.dev.com/images/user/default.jpg';
    const DEFAULT_HEADSHOT_ID = '1';

    const SEX_MALE = 1;
    const SEX_FEMALE = 2;
    const SEX_UNKNOWN = 0;

    protected $_name = 'profile';
    protected $_dependentTables = array('profile_ref');

    protected $_userId;
    protected $_userName;
    protected $_sex;
    protected $_headshotUrl;
    protected $_headshotId;
    protected $_birthday;
    protected $_intro;
    protected $_realName;
    protected $_nickName;

    public function createByBiz($userId, $userName, $nickName='')
    {/*{{{*/
        ParamsChecker::requireNotZero($userId, gettext('userId'));
        ParamsChecker::requireNotNull($userName, gettext('userName'));

        $createTime = $lastModifyTime = date('Y-m-d H:i:s');
        if ('' == $nickName)
        {
            $nickName = $userName;
        }
        $arr['id'] = self::createId();
        $arr['ver'] = self::INITIALIZED_VERSION;
        $arr['createTime'] = $createTime;
        $arr['lastModifyTime'] = $lastModifyTime;
        $arr['userId'] = $userId;
        $arr['userName'] = $userName;
        $arr['sex'] = self::SEX_UNKNOWN;
        $arr['nickName'] = $nickName;
        $arr['headshotUrl'] = self::DEFAULT_HEADSHOT_URL;
        $arr['headshotId'] = self::DEFAULT_HEADSHOT_ID;
        return $this->create($arr);
    }/*}}}*/

        //all setter MUST be declared! DO NOT assign it directly!
    public function setHeadShot($headshot)
    {/*{{{*/
        if (isset($headshot['imgurl']) && isset($headshot['imgid']))
        {
            $this->_headshotUrl = $headshot['imgurl'];
            $this->_headshotId = $headshot['imgid'];
        }
    }/*}}}*/

    public function setNickName($nickName)
    {/*{{{*/
        if ('' == trim($nickName))
        {
            $nickName = $this->_userName;
        }
        $this->_nickName = $nickName;
    }/*}}}*/

    public function setBirthday($birthday)
    {/*{{{*/
        $this->_birthday = $birthday;
    }/*}}}*/

    public function setRealName($realName)
    {/*{{{*/
        $this->_realName = $realName;
    }/*}}}*/

    public function setIntro($intro)
    {/*{{{*/
        $this->_intro = $intro;
    }/*}}}*/

    public function setSex($sex)
    {/*{{{*/
        $this->_sex = $sex;
    }/*}}}*/
    
    public function findByUserId($userId)
    {/*{{{*/
        $db = $this->getAdapter();
        $where = $db->quoteInto('userId= ?', $userId);
        return $this->fetchRow($where);
    }/*}}}*/

    public function findByUserName($userName)
    {/*{{{*/
        $db = $this->getAdapter();
        $where = $db->quoteInto('userName = ?', $userName);
        return $this->fetchRow($where);
    }/*}}}*/
    
    public function batchGetByUserId($userIds)
    {/*{{{*/
        $db = $this->getAdapter();
        $where = $db->quoteInto('userId in (?)', $userIds);
        return $this->fetchAll($where);
    }/*}}}*/

    public function save()
    {
        //save to db
        parent::save();

        //refresh session
        $sess = $this->getSessSvc();
        $info = $sess->get('info');
        $info['nickName'] = $this->_nickName;
        $info['sex'] = $this->_sex;
        $info['realName'] = $this->_realName;
        $info['intro'] = $this->_intro;
        $info['birthday'] = $this->_birthday;

        $sess->save('info', $info);

    }

    protected function map()
    {/*{{{*/
        return array('userId', 'userName', 'sex', 'nickName', 'realName', 'headshotUrl', 'headshotId', 'birthday', 'intro');
    }/*}}}*/
}
